package com.pz.cloud.framework.material.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pz.cloud.framework.business.material.entity.Material;
import com.pz.cloud.framework.business.material.entity.MaterialApply;
import com.pz.cloud.framework.business.material.entity.MaterialApplyDetail;
import com.pz.cloud.framework.business.material.service.IMaterialApplyDetailService;
import com.pz.cloud.framework.business.material.service.IMaterialApplyService;
import com.pz.cloud.framework.business.material.service.IMaterialService;
import com.pz.cloud.framework.business.workflow.annotation.AssembleDealFlag;
import com.pz.cloud.framework.business.workflow.annotation.Workflow;
import com.pz.cloud.framework.material.callback.MaterialApplyCallback;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 场地物资申请
 * @Author: jeecg-boot
 * @Date:   2020-11-28
 * @Version: V1.0
 */
@Api(tags="场地物资申请")
@RestController
@RequestMapping("/materialApply")
@Slf4j
public class MaterialApplyController extends BaseController<MaterialApply, IMaterialApplyService> {
	@Autowired
	private IMaterialApplyService materialApplyService;

	@Autowired
	private IMaterialService materialService;
	@Autowired
	private IMaterialApplyDetailService materialApplyDetailService;

	/**
	 * 分页列表查询
	 *
	 * @param material
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "场地物资申请-分页列表查询")
	@ApiOperation(value="场地物资申请-分页列表查询", notes="场地物资申请-分页列表查询")
	@GetMapping(value = "/list")
	@AssembleDealFlag
	@PermissionData(pageComponent = "material/MaterialApply")
	public Result<?> queryPageList(MaterialApply material,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		IPage<MaterialApply> list = materialApplyService.findPage(material, req.getParameterMap());
		return Result.OK(list);
	}
	
	/**
	 *   添加
	 *
	 * @param material
	 * @return
	 */
	@AutoLog(value = "场地物资申请-添加/编辑")
	@ApiOperation(value="场地物资申请-添加/编辑", notes="场地物资申请-添加/编辑")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody MaterialApply material) {
		materialApplyService.add(material);
		return Result.OK("保存成功！");
	}
	
//	/**
//	 *  编辑
//	 *
//	 * @param material
//	 * @return
//	 */
//	@AutoLog(value = "场地物资申请-编辑")
//	@ApiOperation(value="场地物资申请-编辑", notes="场地物资申请-编辑")
//	@PutMapping(value = "/edit")
//	public Result<?> edit(@RequestBody MaterialApply material) {
//		materialApplyService.updateById(material);
//		return Result.OK("编辑成功!");
//	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "场地物资申请-通过id删除")
	@ApiOperation(value="场地物资申请-通过id删除", notes="场地物资申请-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		materialApplyService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "场地物资申请-批量删除")
	@ApiOperation(value="场地物资申请-批量删除", notes="场地物资申请-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.materialApplyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "场地物资申请-通过id查询")
	@ApiOperation(value="场地物资申请-通过id查询", notes="场地物资申请-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		MaterialApply materialApply = materialApplyService.getById(id);

		if(materialApply==null) {
			return Result.error("未找到对应数据");
		}
		else {
			List<MaterialApplyDetail> details = materialApplyDetailService.findByApplyId(id);
			materialApply.setDetails(details);
//			Material byId = materialService.getById(materialApply.getMaterialId());
//			if (byId != null) {
//				materialApply.setMaterialName(byId.getName());
//				materialApply.setMaterialType(byId.getType());
//				materialApply.setMaterialCode(byId.getCode());
//			}
			return Result.OK(materialApply);
		}
	}

    /**
    * 导出excel
    *
    * @param request
    * @param material
    */
    @RequestMapping(value = "/exportXls")
	@PermissionData(pageComponent = "material/MaterialApply")
	public ModelAndView exportXls(HttpServletRequest request, MaterialApply material) {
		return super.exportXls(request, material, MaterialApply.class, "场地物资申请");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MaterialApply.class);
    }

}
